package com.lyshop.jinxiang.activitys;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.bean.ShopGoodsListBean;
import com.lyshop.jinxiang.utils.GlideUtils;
import com.lyshop.jinxiang.utils.RouteUtil;
import com.lyshop.jinxiang.utils.Utils;
import com.lyshop.jinxiang.view.MoneyTextView;
import com.zc.mychart.utils.FormatUtil;

public class GoodsImageDetailActivity extends BaseActivity {
    private ShopGoodsListBean mBean;
    private TextView tv_go_trade;
    private TextView tv_goods_name;
    private MoneyTextView tv_goods_money;
    private ImageView iv_goods_img;
    private ImageView iv_detail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBean = (ShopGoodsListBean) getIntent().getSerializableExtra(RouteUtil.BUNDLE_HOME_GOODS_MODEL);
        initData();
    }

    private void initData() {
        if (null != mBean) {
            Log.e("art", mBean.goods_content_img);
            tv_goods_money.setText(FormatUtil.formatDouble2(mBean.price));
            tv_goods_name.setText(mBean.name);

            Glide.with(mContext).load(mBean.goods_content_img).apply(GlideUtils.getImageDefaultOptions()).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    int width = resource.getIntrinsicWidth();
                    int height = resource.getIntrinsicHeight();
                    int[] windowWidth = Utils.getWindowWidth(GoodsImageDetailActivity.this);

                    int wWidth = windowWidth[0];
                    Double wHeight = Double.valueOf(windowWidth[1]);

                    double scale = wHeight / wWidth;
                    if (scale > 1.7f) {
                        double widthScale = Double.valueOf(windowWidth[0]) / width;
                        height = (int) (height * widthScale);
                    }

                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(windowWidth[0], height);
                    iv_detail.setLayoutParams(params);
                    iv_detail.setImageDrawable(resource);
                }
            });

            Glide.with(mContext).load(mBean.goods_img).apply(GlideUtils.getImageDefaultOptions()).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    int width = resource.getIntrinsicWidth();
                    int height = resource.getIntrinsicHeight();
                    int[] windowWidth = Utils.getWindowWidth(GoodsImageDetailActivity.this);

                    int wWidth = windowWidth[0];
                    Double wHeight = Double.valueOf(windowWidth[1]);

                    double scale = wHeight / wWidth;
                    if (scale > 1.7f) {
                        double widthScale = Double.valueOf(windowWidth[0]) / width;
                        height = (int) (height * widthScale);
                    }

                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(windowWidth[0], height);
                    iv_goods_img.setLayoutParams(params);
                    iv_goods_img.setImageDrawable(resource);
                }
            });
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_goods_image_detail;
    }

    @Override
    protected void findViews() {
        iv_goods_img = findViewById(R.id.iv_goods_img);
        tv_goods_money = findViewById(R.id.tv_goods_money);
        tv_goods_name = findViewById(R.id.tv_goods_name);
        iv_detail = findViewById(R.id.iv_detail);
        tv_go_trade = findViewById(R.id.tv_go_trade);
    }

    @Override
    protected void setListeners() {
        tv_go_trade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUserValidLogin()) {
                    RouteUtil.toHomeAddGoodsOrderActivity(GoodsImageDetailActivity.this, mBean);
                }
            }
        });
    }
}